package com.shop.shoporder.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.entity.order.Area;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.service.AreaService;
import com.shop.shoporder.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/area")
public class AreaController {
    @Autowired
    AreaService areaService;

    @Autowired
    UserAddressService userAddressService;

    @ResponseBody
    @RequestMapping(value = "/getArea", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response getArea(HttpServletRequest request, @RequestBody JSONObject jsonParam){
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = map.get("parm").toString();
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
            try {
                String res = map.get("parm").toString();
                //System.out.println("res:"+res);

                int AID  = Integer.parseInt(map.get("AID").toString());
                JSONObject param = JSONObject.parseObject(res);
//                Integer parentIdStr=(Integer) param.get("parentId");
//                String levelStr=(String) param.get("level");
//                System.out.println("parentIdStr:"+parentIdStr);
//                System.out.println("levelStr:"+levelStr);
//                Map<String,String> mapPa=new HashMap<>();
//                mapPa.put("parentId",(String) param.get("parentId"));
//                mapPa.put("level",(String) param.get("level"));
//                System.out.println("parentId:"+(String)param.get("parentId"));
//                System.out.println("level:"+(String)param.get("level"));
                int parentId=0;
                int level=0;
                try {

                    parentId=Integer.parseInt((String) param.get("parentId"));
                    level=Integer.parseInt((String)param.get("level"));
                }catch (Exception e){}
                //读取数据表
                List<Area> areaList = areaService.selectAreaList(parentId,level);
                if (areaList != null&&areaList.size()>0) {
                    return new Response(MessageConstants.MSG_SUCCESS, areaList, areaList.size(), "", request);
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "读取数据错误1", request);
            }
        }
    }
}
